package net.zhuike.novel.app.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.util.Log;

import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;

import framework.common.zanetwork.UeHttpUrl;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 加载配置文件的后台服务
 *
 * @author wq
 */
public class UpdateConfigService extends Service {
    /**
     * 是否继续运行线程
     */
    private boolean isRunning = true;
    private OkHttpClient mOkHttpClient;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    private void getConfig() {
        mOkHttpClient = new OkHttpClient();
        try {
            final Request request = new Request.Builder()
                    .url(UeHttpUrl.setting())
                    .get()
                    .build();
            Call call = mOkHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String str = response.body().string();
                    try {
                        JSONObject restr = new JSONObject(str);
                        SharedPreferencesUtil.getInstance().putString("setJson",str);
                        JSONObject obj=restr.getJSONObject("adsenseSetting");
                        JSONObject objs=obj.getJSONObject("appStart");
                        String appStart=objs.getString("enable");
                        SharedPreferencesUtil.getInstance().putString("appStart", appStart);
                        JSONObject perChapterStartobjs=obj.getJSONObject("perChapterStart");
                        String perChapterStart=perChapterStartobjs.getString("enable");
                        SharedPreferencesUtil.getInstance().putString("perChapterStart", perChapterStart);
                        JSONObject chapterBottomobjs=obj.getJSONObject("chapterBottom");
                        String chapterBottom=chapterBottomobjs.getString("enable");
                        SharedPreferencesUtil.getInstance().putString("chapterBottom", chapterBottom);
                        JSONObject perChapterEndPopobjs=obj.getJSONObject("perChapterEndPop");
                        String perChapterEndPop=perChapterEndPopobjs.getString("enable");
                        SharedPreferencesUtil.getInstance().putString("perChapterEndPop", perChapterEndPop);
                        JSONObject listPopobjs=obj.getJSONObject("list");
                        String listEndPop=listPopobjs.getString("enable");
                        SharedPreferencesUtil.getInstance().putString("listEndPop", listEndPop);
                        if (restr.has("topCategoryId")) {
                            String topCategoryId = restr.getString("topCategoryId");
                            SharedPreferencesUtil.getInstance().putString(Constant.TopCategoryId, topCategoryId);
                        }
                        if (restr.has("dataUrl")) {
                            if (!restr.getString("dataUrl").equals("")) {
                                UeHttpUrl.SERVER_IP = restr.getString("dataUrl");
                            }
                        }
                        if(restr.has("email")){
                            SharedPreferencesUtil.getInstance().putString(Constant.email, restr.getString("email"));
                        }
                        stopSelf();
                        isRunning = false;
                    } catch (JSONException e1) {
                        e1.printStackTrace();
                    }
                }

            });
        } catch (Exception e) {

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isRunning = false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (isRunning) {
            getConfig();

        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}


